﻿using System;
using Ninject;
using System.Web.Mvc;
using Example.Base.Interfaces.Services;

namespace Example.Site.Attributes
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class ValidateActivationAttribute : ActionFilterAttribute
    {
        private readonly String _redirectTo;


        public ValidateActivationAttribute()
        {
            _redirectTo = "~/Error/ActionDisabled";
        }

        public ValidateActivationAttribute(string redirectTo)
        {
            _redirectTo = redirectTo;
        }

        [Inject]
        public IConfigurationService ConfigurationService { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var actionName = filterContext.ActionDescriptor.ActionName;
            var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;

            var isValid = Boolean.Parse(ConfigurationService.GetValue(String.Format("{0}/{1}", controllerName, actionName)));

            if (!isValid)
            {
                filterContext.Result = new RedirectResult(_redirectTo);
            }
        }

    }
}
